package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.impl.PlanDaoImpl;
import cn.topevery.garden.dal.logic.PlanFileLogic;
import cn.topevery.garden.dal.logic.PlanLogic;
import cn.topevery.garden.dal.util.QueryWrapper;
import cn.topevery.garden.entity.dto.PlanDTO;
import cn.topevery.garden.entity.po.Plan;
import cn.topevery.garden.entity.query.PlanQuery;
import cn.topevery.garden.entity.vo.PestAnalyzeVO;
import cn.topevery.garden.entity.vo.PlanVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.entity.po.SysDept;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "养护计划管理")
@RestController
@RequestMapping("/plan")
public class PlanController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    PlanLogic planLogic;

    @Autowired
    PlanDaoImpl planDao;

    @Autowired
    PlanFileLogic planFileLogic;

    @PostMapping(value = "/commit")
    @ApiOperation(value = "养护计划新增和修改接口")
    public JsonResult<?> commit(@RequestBody PlanVO vo) {
        SysUser user = getUser();
        SysDept dept = this.getSysDept();
        vo.setCompanyId(dept.deptId);
        vo.setCompanyName(dept.deptShortName);
        vo.setMapId(user.mapId);
        planLogic.commit(vo, user.id.toString());
        return JsonResult.ok(true);
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "养护计划分页查询")
    @PostMapping("/getPages")
    public JsonResult<DataPager<PlanVO>> listPage(@RequestBody PlanQuery query) {
        return JsonResult.ok(planLogic.listPage(query));
    }

    @ApiOperation("查询已下发的养护计划")
    @PostMapping("/getSendPlanPage")
    public JsonResult<DataPager<PlanVO>> getSendPlanPage(@RequestBody PlanQuery query) {
        return JsonResult.ok(planLogic.getSendPlanPage(query, getUser()));
    }

    @GetMapping(value = "/delete")
    @ApiOperation(value = "批量删除")
    public JsonResult<?> delete(String planId) {
        SysUser user = getUser();
        planLogic.deleteByIds(planId, user.userId);
        return JsonResult.ok("删除成功");
    }

    @GetMapping(value = "/getByName")
    @ApiOperation(value = "通过planId获取养护计划")
    public JsonResult<PlanVO> getByName(String name) {
        List<Plan> plan = planLogic.getByName(name);
        PlanVO res = planLogic.getGroupByName(name);
        res.setPlanList(plan);
        res.setFiles(planFileLogic.list(res.getPlanId()));
        return JsonResult.ok(res);
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id获取养护计划")
    public JsonResult<?> getById(Integer id) {
        Plan plan = planLogic.getPlanInfo(id);
        plan.setFileList(planFileLogic.list(id));
        return JsonResult.ok(plan);
    }

    @GetMapping(value = "/getPestPlanList")
    @ApiOperation(value = "获取病虫害点位信息")
    public JsonResult<?> getPestPlanList() {
        List<Plan> list = planLogic.getPestPlanList();
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/mainPlanApprove")
    @ApiOperation(value = "主计划审核")
    public JsonResult<?> mainPlanApprove(String planId) {
        planLogic.mainPlanApprove(planId, getUser().userId);
        return JsonResult.ok(true);
    }

    @GetMapping(value = "/mainPlanConfirm")
    @ApiOperation(value = "主计划确认")
    public JsonResult<?> mainPlanConfirm(String planId) {
        boolean flag = planLogic.mainPlanConfirm(planId, getUser().userId);
        return JsonResult.ok(flag);
    }

    @PostMapping(value = "/subPlanExecute")
    @ApiOperation(value = "子计划执行")
    public JsonResult<?> subPlanExecute(@RequestBody PlanVO vo) {
        planLogic.subPlanExecute(vo, getUser().userId);
        return JsonResult.ok();
    }

    @GetMapping(value = "/getPestDeptInfo")
    @ApiOperation(value = "病虫害统计部门情况")
    public JsonResult<?> getPestDeptInfo(boolean isSum) {
        List<PestAnalyzeVO> list = planLogic.getPestDeptInfo(isSum);
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/getPestAreaInfo")
    @ApiOperation(value = "病虫害统计区域情况")
    public JsonResult<?> getPestAreaInfo(boolean isSum) {
        List<PestAnalyzeVO> list = planLogic.getPestAreaInfo(isSum);
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/getScheduleRecord")
    @ApiOperation(value = "获取养护记录列表")
    public JsonResult<?> getScheduleRecord(String uuid, Integer type) {
        List<Plan> list = planLogic.getScheduleRecord(uuid, type);
        return JsonResult.ok(list);
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody PlanDTO dto) {
        planLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, Plan.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<PlanVO> getInfoById(@RequestParam Integer id) {
        Plan entity = planLogic.getEntity(id);
        PlanVO vo = mapper.map(entity, PlanVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        planLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(PlanQuery query, HttpServletResponse response) {
        List<PlanVO> list = planLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", PlanVO.class, "", response);
    }

    @ApiOperation(value = "任务下发")
    @PostMapping(value = "/sendTask")
    public JsonResult<?> sendTask(@RequestBody PlanDTO dto) {
        planDao.updateByTable(getUser().id.toString(),
                new QueryWrapper<KeyValue>().eq("c_send", 1)
                        .eq("c_send_type", dto.getSendType())
                        .eq("c_send_obj", dto.getSendObj())
                        .eq("c_send_obj_name", dto.getSendObjName()),
                new QueryWrapper<KeyValue>().where("c_plan_id", dto.getPlanId()));
        return JsonResult.ok(true);
    }

    @ApiOperation(value = "提醒")
    @PostMapping(value = "/getNotifyMsg")
    public JsonResult<?> getNotifyMsg() {
        return JsonResult.ok(planDao.getNotifyMsg());
    }

    @ApiOperation("获取所有数树木的养护计划")
    @PostMapping("/getTreePlanList")
    public JsonResult<DataPager<PlanVO>> getTreePlanList(PlanQuery query) {
        return JsonResult.ok(planLogic.getTreePlanList(query));
    }

}
